package com.cbgolf.oa.manager;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.TextView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.CalendarAdapter;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.DensityUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;
import java.util.Calendar;
import java.util.List;

/**
 * @author lq
 * @date 2018/4/17
 */

public class DialogManager {

  private DialogManager() {

  }

  public static DialogManager getInstance() {
    return Inner.manager;
  }

  private static class Inner {

    public static DialogManager manager = new DialogManager();
  }

  public String title;
  public String contentText;
  public String cancelText;
  public String submitText;
  public IDialogManagerWaiter worker;


  public void release() {
    if (null != permissionDialog) {
      permissionDialog.dismiss();
      permissionDialog = null;
    }
    if (null != settingDialog) {
      settingDialog.dismiss();
      settingDialog = null;
    }
    worker = null;
    if (null != dialogCalendar && dialogCalendar.isShowing()) {
      dialogCalendar.dismiss();
    }
    dialogCalendar = null;
  }

  public DialogManager setTitle(String title) {
    this.title = title;
    return this;
  }

  public DialogManager setContentMsg(String content) {
    this.contentText = content;
    return this;
  }

  public DialogManager setCancelText(String cancel) {
    this.cancelText = cancel;
    return this;
  }

  public DialogManager setSubmitText(String submitText) {
    this.submitText = submitText;
    return this;
  }

  public DialogManager setWorker(IDialogManagerWaiter worker) {
    this.worker = worker;
    return this;
  }

  public interface IDialogManagerWaiter {

    void doSubmit();

    void doCancel();
  }


  public void showChooseDialog(Activity context) {
    final Dialog dialog = new Dialog(context, R.style.mDialog);
    View view = ViewUtils.inflate(context, R.layout.dialog_gongyong);
    TextView cancelTv = ButterKnife.findById(view, R.id.dialog_g_cancel_tv);
    TextView subMitTv = ButterKnife.findById(view, R.id.dialog_g_submit_tv);
    TextView titleTv = ButterKnife.findById(view, R.id.dialog_g_title_tv);
    TextView contentTv = ButterKnife.findById(view, R.id.dialog_g_content_tv);
    View layout = ButterKnife.findById(view, R.id.dialog_g_layout_ll);
    ViewUtils.setLayoutParams(layout, ScreenUtil.getWidth() * 4 / 5, LayoutParams.WRAP_CONTENT);
    AutoUtil.auto(view);
    ViewUtils.setText(titleTv, title, false);
    ViewUtils.setText(cancelTv, cancelText, false);
    ViewUtils.setText(subMitTv, submitText, false);
    ViewUtils.setText(contentTv, contentText, false);
    dialog.setContentView(view);
    cancelTv.setOnClickListener(v -> {
      dialog.dismiss();
      if (null != worker) {
        worker.doCancel();
      }
    });
    subMitTv.setOnClickListener(v -> {
      dialog.dismiss();
      if (null != worker) {
        worker.doSubmit();
      }

    });
    dialog.show();
  }

  private Dialog permissionDialog;

  // 提示用户该请求权限的弹出框
  public void showRequestPermissionDialog(Context context,
      List<String> listPermissionNames, IDialogManagerWaiter waiter) {
    if (null != permissionDialog && permissionDialog.isShowing()) {
      return;
    }
    permissionDialog = new Dialog(context, R.style.mDialog);
    permissionDialog.setContentView(R.layout.dialog_modify);
    View layout = permissionDialog.findViewById(R.id.dialog_modify_layout_ll);

    ViewUtils.setLlLayoutParams(layout,
        ScreenUtil.getWidth() - DensityUtil.dip2px(context, 58),
        LayoutParams.WRAP_CONTENT);
    TextView titileTv = permissionDialog.findViewById(R.id.dialog_modify_title_tv);
    TextView contentTv = permissionDialog.findViewById(R.id.dialog_modify_content_tv);
    TextView sureTv = permissionDialog.findViewById(R.id.dialog_modify_sure_tv);
    TextView cancelTv = permissionDialog.findViewById(R.id.dialog_modify_cancel_tv);
    TextView listTv = permissionDialog.findViewById(R.id.dialog_modify_list_tv);
    titileTv.setText("权限不可用");

    String pNames = null;
    if (!Util.listIsNull(listPermissionNames)) {
      StringBuilder sb = new StringBuilder();
      for (String s : listPermissionNames) {
        sb.append(s).append("、");
      }
      if (!Util.isNull(sb.toString()) && sb.toString().endsWith("、")) {
        pNames = sb.toString().substring(0, sb.toString().length() - 1);
      }
    }
    titileTv.setText("权限不可用");
    contentTv.setText("当前缺少必要功能权限，部分功能无法正常使用！请开启以下权限:");
    ViewUtils.setText(listTv, pNames);
    sureTv.setText("立即开启");
    cancelTv.setText("稍后再说");
    sureTv.setOnClickListener(v -> {
      permissionDialog.cancel();
      permissionDialog.dismiss();
      if (null != waiter) {
        waiter.doSubmit();
      }
    });
    cancelTv.setOnClickListener(v -> {
      permissionDialog.cancel();
      permissionDialog.dismiss();
      if (null != waiter) {
        waiter.doCancel();
      }
    });
    permissionDialog.setCancelable(false);
    permissionDialog.show();
  }

  private Dialog settingDialog;

  // 提示用户去应用设置界面手动开启权限
  public void showSettingDialog(Context context, List<String> listPermissionNames,
      IDialogManagerWaiter waiter) {
    if (null != settingDialog && settingDialog.isShowing()) {
      return;
    }
    settingDialog = new Dialog(context, R.style.mDialog);
    settingDialog.setContentView(R.layout.dialog_modify);
    View layout = settingDialog.findViewById(R.id.dialog_modify_layout_ll);
    ViewUtils.setLlLayoutParams(layout,
        ScreenUtil.getWidth() - DensityUtil.dip2px(context, 58), LayoutParams.WRAP_CONTENT);
    TextView titileTv = settingDialog.findViewById(R.id.dialog_modify_title_tv);
    TextView contentTv = settingDialog.findViewById(R.id.dialog_modify_content_tv);
    TextView sureTv = settingDialog.findViewById(R.id.dialog_modify_sure_tv);
    TextView cancelTv = settingDialog.findViewById(R.id.dialog_modify_cancel_tv);
    TextView listTv = settingDialog.findViewById(R.id.dialog_modify_list_tv);
    titileTv.setText("权限不可用");
    String pNames = null;
    if (!Util.listIsNull(listPermissionNames)) {
      StringBuilder sb = new StringBuilder();
      for (String s : listPermissionNames) {
        sb.append(s).append("、");
      }
      if (!Util.isNull(sb.toString()) && sb.toString().endsWith("、")) {
        pNames = sb.toString().substring(0, sb.toString().length() - 1);
      }
    }
    contentTv.setText("当前缺少必要权限，可能导致部分功能暂时无法使用。如若需要，请单击【去设置】按钮前往设置中心进行权限授权。");
    ViewUtils.setText(listTv, pNames);
    sureTv.setText("去设置");
    cancelTv.setText("稍后再说");

    sureTv.setOnClickListener(v -> {
      settingDialog.cancel();
      settingDialog.dismiss();
      if (null != waiter) {
        waiter.doSubmit();
      }
    });
    cancelTv.setOnClickListener(v -> {
      settingDialog.cancel();
      settingDialog.dismiss();
      if (null != waiter) {
        waiter.doCancel();
      }
    });
    settingDialog.setCancelable(false);
    settingDialog.show();


  }

  public void showSettingDialog(Context context, boolean isDismiss, IDialogManagerWaiter worker) {
    showSettingDialog(context, "", isDismiss, worker);
  }


  // 提示用户去应用设置界面手动开启权限
  public void showSettingDialog(Context context, String content, boolean isDismiss,
      IDialogManagerWaiter waiter) {
    if (null != settingDialog && settingDialog.isShowing()) {
      return;
    }
    settingDialog = new Dialog(context, R.style.mDialog);
    settingDialog.setContentView(R.layout.dialog_modify);
    View layout = settingDialog.findViewById(R.id.dialog_modify_layout_ll);
    ViewUtils.setLlLayoutParams(layout,
        ScreenUtil.getWidth() - DensityUtil.dip2px(context, 58), LayoutParams.WRAP_CONTENT);
    TextView titileTv = settingDialog.findViewById(R.id.dialog_modify_title_tv);
    TextView contentTv = settingDialog.findViewById(R.id.dialog_modify_content_tv);
    TextView sureTv = settingDialog.findViewById(R.id.dialog_modify_sure_tv);
    TextView cancelTv = settingDialog.findViewById(R.id.dialog_modify_cancel_tv);
    titileTv.setText("温馨提示");
    String msg = "当前缺少必要权限，可能导致部分功能暂时无法使用。如若需要，请单击【去设置】按钮前往设置中心进行权限授权。";
    ViewUtils.setText(contentTv, Util.isNull(content) ? msg : content);
    sureTv.setText("去设置");
    cancelTv.setText("稍后再说");
    sureTv.setOnClickListener(v -> {
      if (isDismiss) {
        settingDialog.dismiss();
      }
      if (null != waiter) {
        waiter.doSubmit();
      }
    });
    cancelTv.setOnClickListener(v -> {
      settingDialog.cancel();
      settingDialog.dismiss();
      if (null != waiter) {
        waiter.doCancel();
      }
    });
    settingDialog.setCancelable(false);
    settingDialog.show();


  }

  public void showMsgDialog(Activity act, String title, String content,
      final IDialogManagerWaiter listener) {
    final Dialog dialog = new Dialog(act, R.style.mDialog);
    View view = act.getLayoutInflater().inflate(R.layout.dialog_msg, null);
    View layout = view.findViewById(R.id.dialog_layout);
    TextView titleTv = view.findViewById(R.id.dialog_title_tv);
    TextView msgTv = view.findViewById(R.id.dialog_msg_tv);
    TextView cancelTv = view.findViewById(R.id.dialog_cancel_tv);
    TextView subMitTv = view.findViewById(R.id.dialog_submit_tv);
    ViewUtils.setLayoutParams(layout, ScreenUtil.getWidth() * 3 / 4, LayoutParams.WRAP_CONTENT);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    if (!Util.isNull(title)) {
      titleTv.setText(title);
    }
    if (!Util.isNull(content)) {
      ViewUtils.setText(msgTv, content);
    }
    cancelTv.setOnClickListener(v -> {
      dialog.dismiss();
      if (null != listener) {
        listener.doCancel();


      }
    });
    subMitTv.setOnClickListener(v -> {
      dialog.dismiss();
      if (null != listener) {
        listener.doSubmit();
      }
    });
    dialog.show();
  }

  private int tempYear;
  private int tempMonth;
  private Dialog dialogCalendar;

  public void showCalendarDialog(Activity context, long time) {
    if (null != dialogCalendar && dialogCalendar.isShowing()) {
      return;
    }
    if (time <= 0) {
      return;
    }
    dialogCalendar = new Dialog(context, R.style.mDialog);
    View view = context.getLayoutInflater().inflate(R.layout.dialog_calendar_phone, null);
    View before = view.findViewById(R.id.dialog_calendar_before_ll);
    View after = view.findViewById(R.id.dialog_calendar_after_ll);
    final TextView title = view.findViewById(R.id.dialog_calendar_title_tv);
    final GridView gridView = view.findViewById(R.id.dialog_calendar_gridview);
    AutoUtil.auto(view);
    dialogCalendar.setContentView(view);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    final int curYear = calendar.get(Calendar.YEAR);
    final int curMonth = calendar.get(Calendar.MONTH) + 1;
    tempYear = DateUtil.getYear(time);
    tempMonth = DateUtil.getMonth(time);
    ViewUtils.setText(title, tempMonth + "月 " + tempYear + "年");
    final CalendarAdapter adapterCalendar = new CalendarAdapter(context, time);
    gridView.setAdapter(adapterCalendar);
    after.setOnClickListener(v -> {
      tempMonth = tempMonth + 1;
      if (tempMonth > 12) {
        tempMonth = 1;
        tempYear += 1;
      }
      ViewUtils.setText(title, tempMonth + "月 " + tempYear + "年");
      adapterCalendar.clear();
      adapterCalendar.setData(tempYear, tempMonth);
      adapterCalendar.upDataUI();
    });
    before.setOnClickListener(v -> {
      tempMonth = tempMonth - 1;
      if (tempMonth == 0) {
        tempMonth = 12;
        tempYear = tempYear - 1;
      }
      if (tempYear < curYear || (tempYear == curYear && tempMonth < curMonth)) {
        tempMonth += 1;
        if (tempMonth > 12) {
          tempMonth = 1;
          tempYear += 1;
        }
        return;
      }
      ViewUtils.setText(title, tempMonth + "月 " + tempYear + "年");
      adapterCalendar.clear();
      adapterCalendar.setData(tempYear, tempMonth);
      adapterCalendar.upDataUI();
    });

    dialogCalendar.show();
  }
}
